-
Notifications
You must be signed in to change notification settings - Fork 133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add length check (setting it) for sequences #1117
Conversation
For now, sequences that are too long are allowed. We might want to warn in these cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your effort.
src/oemof/solph/_plumbing.py
Outdated
|
||
if isinstance(sequence, _FakeSequence): | ||
sequence.size = length | ||
return True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would check it differently:
if isinstance(sequence, _FakeSequence):
if sequence.size is None:
return True
else:
if sequence.size == length:
return True
else:
return False
For me a sequence fits for any size, therefore, it is always TRUE, regardless of the checked length.
This behaviour will change if you set a length. From this moment on the size is set. If you now check the wrong length it is not valid any more.
I do not like the hidden setter. I would not expect the function to set the size of the sequence.
If you really want this hidden magic, I would prefer the following:
if isinstance(sequence, _FakeSequence):
if sequence.size is None:
sequence.size = length
return True
else:
if sequence.size == length:
return True
else:
return False
This will fail if I ask for a different length later. In your function the size will be updated, even if the check ask for different length.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand what you are saying. Still, I tried to avoid this being a "hidden setter": According to the docstring, valid_sequence
explicitly makes the argument a valid sequence of the given length. I did this to solve #1115: In your results, you might want to iterate over all values, real and fake sequences, in the same way.
I see three ways of doing this:
- Proceed as suggested in this PR.
- Rename the function
valid_sequence
to something likeassure_valid_sequence
to emphasize possible changes by the function. - Make this even more explicit, e.g. by
a. using the explicit setter (_Fakesequence.size = length
) when needed, or by
b. renamingvalid_sequence
tomake_valid_sequence
. This would then raise an Error instead of returning False. - Completely remove the
_FakeSequence
and keep scalars. Then, cast on demand (to an np.array).
I'd opt to do something quick for the v0.5 release (so that the old behavior is maintained) and to remove the _FakeSequence
in v0.6.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you.
The test of the 0th element of a sequence being None is replaced by a test function that also sets the length of
_FakeSequence
objects, making them iterable afterwards. This is replacing the old hidden magic of setting the size on read access (from solph v0.5.3) by explicit write access at the same points in the code. (Just once in the beginning instead of multiple times on each access.)Fixes #1115